# 存储名片
class CardsTools:
    card_list = []

    def __init__(self) -> None:
        pass

    def show_menu(self):
        """显示菜单"""
        print("*" * 50)
        print("名片系统")
        print("1:新增")
        print("2:全部")
        print("3:查询")
        print("0:退出")
        print("*" * 50)

    def show_search_menu(self):
        """选择操作"""
        print("*" * 50)
        print("名片系统")
        print("1:修改")
        print("2:删除")
        print("0:返回")
        print("*" * 50)

    def process_search_operation(self, index, dict):
        action_str = input('请输入对操作:')
        if action_str in ['1', '2']:
            if action_str == '1':
                # 修改
                return self.modify_card(index, dict)
                pass
            elif action_str == '2':
                return self.del_card(index)
                pass
            pass
        elif action_str == '0':
            print('返回上一层')
            return False
        else:
            print('输入错误')
            return False

    def print_card(self, dict):
        print("名片: 名字[%s],电话[%s]" % (dict['name'], dict['phone']))

    def new_card(self):
        """创建名片"""
        name = input('姓名:')
        phone = input('电话:')
        card_dict = {"name": name, "phone": phone}
        self.card_list.append(card_dict)
        print("添加成功,名字[%s]" % name)

    def modify_card(self, index, dict):
        """修改名片"""
        dict['name'] = self.input_card(dict['name'], '姓名:')
        dict['phone'] = self.input_card(dict['phone'], '电话:')
        return True

    def del_card(self, index):
        """删除名片"""
        self.card_list.pop(index)
        return True

    def input_card(self, value, tip):
        key = input(tip)
        if (key == ''):
            return value
        return key

    def show_all(self):
        """显示名片"""
        if (len(self.card_list) == 0):
            print('没有名片')
            return
        for dict in self.card_list:
            self.print_card(dict)

    def search_card(self):
        """查询名片"""
        name = input('请输入查询姓名:')
        for index, dict in enumerate(self.card_list):
            if (dict['name'] == name):
                self.print_card(dict)
                operation = True
                while(operation):
                    self.show_search_menu()
                    operation = self.process_search_operation(index, dict)
                break
        else:
            print('没找到名片')
